<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="手机号">
              <el-input v-model="form.username" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态">
              <el-select v-model="form.status" placeholder="请选择">
                <el-option
                  v-for="item in statusOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="账号类型">
              <el-select v-model="form.accountType" placeholder="请选择">
                <el-option
                  v-for="item in accountOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button style="margin-left:60px" type="primary" @click="searchFn">查询</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <!-- table列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="username" label="手机号">
          <template slot-scope="scope">
            <div>{{scope.row.username | phonereg }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="oldRoleName" label="原角色" />
        <el-table-column prop="newRoleName" label="新角色" />
        <el-table-column prop="createdAt" label="提交时间" />
        <el-table-column prop="examineTime" label="生效时间" />
        <el-table-column prop="adminSubmit" label="后台提交人" />
        <el-table-column prop="adminExamine" label="后台审核人" />
        <el-table-column label="修改来源">
          <template slot-scope="scope">
            <div>{{ scope.row.type | typeFn }}</div>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <div>{{ scope.row.status | statusFn }}</div>
          </template>
        </el-table-column>
        <el-table-column label="账号类型">
          <template slot-scope="scope">
            <div>{{ scope.row.accountType | accountFn }}</div>
          </template>
        </el-table-column>
        <el-table-column
          v-if="hasPermission('AdminUserRoleTransProvider.doAudit')"
          fixed="right"
          label="操作"
          width="100"
        >
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status == 0"
              type="text"
              @click="
                auditBtn(
                  scope.row.id,
                  scope.row.takeEffect,
                  scope.row.examineTime,
                  scope.row.signType
                )
              "
            >审核</el-button>
            <span v-else>已审核</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 审核弹出窗 -->
      <el-dialog title="审核" :visible.sync="dialogs" width="480px" :before-close="handleCloses">
        <el-form
          :inline="true"
          label-width="100px"
          :rules="rules"
          ref="auditForm"
          :model="auditForm"
        >
          <el-row>
            <el-col :span="24">
              <el-form-item label="审核状态">
                <el-select v-model="auditForm.status" placeholder="请选择">
                  <el-option
                    v-for="item in staOption"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="生效日期">
                <span v-if="auditForm.takeEffect == 1">立即生效</span>
                <el-date-picker
                  v-else
                  v-model="auditForm.examineTime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  type="datetime"
                  placeholder="选择日期时间"
                />
              </el-form-item>
            </el-col>
            <el-col v-if="auditForm.signType == 1" :span="24">
              <el-form-item label="是否回款" prop="returnMoney">
                <el-select v-model="auditForm.returnMoney" placeholder="请选择">
                  <el-option label="是" :value="1" />
                  <el-option label="否" :value="0" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col v-if="auditForm.signType == 1" :span="24">
              <el-form-item label="回款备注">
                <el-input v-model="auditForm.returnRemark" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogs = false">取 消</el-button>
          <el-button type="primary" @click="auditFn('auditForm')">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="listForm.pageNum"
            :page-sizes="page_sizes"
            :page-size="listForm.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { selectUserRoleTransAuditList, doAudit } from "@/api/roleAudit";

export default {
  name: "RoleAuditIndex",
  filters: {
    cardType: function (e) {
      return window.helper.cardType(String(e));
    },
    typeFn(e) {
      switch (e) {
        case 1:
          return "后台";
        case 2:
          return "电子协议";
        default:
          return "暂无";
      }
    },
    statusFn(e) {
      switch (e) {
        case 0:
          return "待审核";
        case 1:
          return "审核通过";
        case 2:
          return "审核驳回";
        case 3:
          return "财务通过";
        default:
          return "暂无";
      }
    },
    accountFn(e) {
      console.log(e);
      switch (e) {
        case 1:
          return "一般账号";
        case 2:
          return "员工";
        case 3:
          return "特批";
        case 4:
          return "试用";
        case 5:
          return "PLUS会员";
        case 6:
          return "颜咖";
        case 8:
          return "定金";
        default:
          return "暂无";
      }
    },
  },
  data() {
    return {
      dialogs: false,
      tableLoading: true,
      form: {
        pageNum: 1,
        pageSize: 10,
      },
      auditForm: {
        id: null,
        takeEffect: null,
        examineTime: null,
        status: null,
      },
      tableData: [],
      roleOptions: [],
      page_sizes: [10, 30, 50, 100],
      page_totals: 60,
      listForm: {
        pageNum: 1,
        pageSize: 10,
      },
      rules: {
        returnMoney: [
          { required: true, message: "请选择是否回款", trigger: "change" },
        ],
      },
      statusOption: [
        {
          value: 0,
          label: "待审核",
        },
        {
          value: 1,
          label: "审核通过",
        },
        {
          value: 2,
          label: "审核驳回",
        },
        {
          value: 3,
          label: "财务通过",
        },
      ],
      staOption: [
        {
          value: 2,
          label: "不通过",
        },
        {
          value: 3,
          label: "通过",
        },
      ],
      accountOption: [
        {
          value: 1,
          label: "一般账号",
        },
        {
          value: 2,
          label: "员工",
        },
        {
          value: 3,
          label: "特批",
        },
        {
          value: 4,
          label: "试用",
        },
        {
          value: 8,
          label: "定金",
        },
      ],
    };
  },
  inject: ["reload"],
  mounted() {
    this.getList(this.listForm);
    this.roleOptions = window.helper.roleFn("listReturn");
  },
  methods: {
    // 按钮权限判断
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },
    // 审核弹窗
    auditBtn(e, takeEffect, examineTime, signType) {
      // signType：1赠送 2购买
      this.auditForm.id = e;
      this.auditForm.takeEffect = takeEffect;
      this.auditForm.examineTime = examineTime;
      this.auditForm.signType = signType;
      console.log(this.auditForm);
      this.dialogs = true;
    },
    handleCloses() {
      this.dialogs = false;
    },
    // 审核
    auditFn(auditForm) {
      console.log(this.auditForm);
      const datas = this.auditForm;
      this.$refs[auditForm].validate((valid) => {
        if (valid) {
          if (this.auditForm.signType == 1) {
            if (
              !this.auditForm.returnRemark &&
              this.auditForm.returnMoney == 1
            ) {
              this.$message.error("回款/回款备注不能为空");
              return;
            }
          }
          doAudit(datas)
            .then((res) => {
              console.log(res);
              if (!res.data.code) {
                this.succStatus("审核");
              } else {
                this.$message.error(res.data.msg);
              }
              this.reload();
            })
            .catch((error) => {
              this.$message.error(error);
            });
        } else {
          this.$message({
            message: "有必填项没填，不能提交咯",
            type: "warning",
          });
        }
      });
    },
    // 成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + "成功",
        type: "success",
      });
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + "失败");
    },
    // 查询
    searchFn() {
      this.form.pageNum = 1;
      // console.log(this.form)
      this.getList(this.form);
    },
    // 列表数据获取
    getList(e) {
      this.tableLoading = true;
      const datas = e;
      selectUserRoleTransAuditList(datas)
        .then((res) => {
          if (!res.data.code) {
            console.log(res);
            this.tableData = res.data.data.list;
            this.page_totals = res.data.data.count;
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch((error) => {
          this.$message.error(error);
        });
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.pageNum = val;
      this.getList(this.form);
    },
  },
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
</style>
